import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:f_demo/commons/index.dart';
import 'package:f_demo/generated/i18n.dart';
import 'package:f_demo/ui/pages/tab_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:oktoast/oktoast.dart';

import 'package:f_demo/ui/pages/index.dart';
import 'package:f_demo/bloc/index.dart';

void main() {
  BlocSupervisor.delegate = AppBlocDelegate();
  runApp(MultiBlocProvider(
    providers: [
      BlocProvider<ThemeBloc>(
        builder: (BuildContext context) => ThemeBloc(),
      ),
      BlocProvider<LanguageBloc>(
        builder: (BuildContext context) => LanguageBloc(),
      ),
      BlocProvider<OauthBloc>(
        builder: (BuildContext context) => OauthBloc(),
      )
    ],
    child: OKToast(child: App()),
  ));
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  ThemeBloc _themeBloc;
  LanguageBloc _languageBloc;

  @override
  void initState() {
    _themeBloc = BlocProvider.of<ThemeBloc>(context);
    _languageBloc = BlocProvider.of<LanguageBloc>(context);
    initLocal();

    super.initState();
  }

  initLocal() async {
    final prefs = await SharedPreferences.getInstance();

    String keyPrimaryColor = prefs.get(Share.primaryColor);
    bool keyBrightness = prefs.get(Share.brightness);
    String local = prefs.get(Share.language);

    if (keyPrimaryColor != null) {
      _themeBloc.dispatch(UpdateThemeEvent(color: keyPrimaryColor));
    }

    if (keyBrightness != null) {
      _themeBloc.dispatch(UpdateBrightnessEvent(brightness: keyBrightness));
    }

    if (local != null) {
      _languageBloc.dispatch(UpdateLanguageEvent(local: local));
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ThemeBloc, ThemeState>(
      builder: (context, themeState) {
        return BlocBuilder<LanguageBloc, LanguageState>(
          builder: (context, localState) {
            return MaterialApp(
              title: "小小帅",
              locale: localState.local,
              localizationsDelegates: [
                I18n.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
              ],
              supportedLocales: I18n.delegate.supportedLocales,
              localeResolutionCallback: I18n.delegate.resolution(fallback: const Locale('en', 'US')),
              theme: ThemeData(
                brightness: themeState.brightness,
                primaryColor: themeState.color,
              ),
              initialRoute: '/',
              routes: {
                Routes.splash: (ctx) => SplashPage(),
                Routes.main: (ctx) => TabPage(),
                Routes.home: (ctx) => HomePage(),
                Routes.settingTheme: (ctx) => SettingPage(),
                Routes.login: (ctx) => LoginPage(),
              },
            );
          },
        );
      },
    );
  }
}

Route<dynamic> createInitialRoute() {
  return PageRouteBuilder(
      transitionDuration: const Duration(seconds: 1),
      pageBuilder: (BuildContext context, _, __) {
        return SplashPage();
      },
      transitionsBuilder: (_, animation, __, child) {
        return RotationTransition(
          turns: Tween(begin: 0.0, end: 1.0).animate(animation),
          child: ScaleTransition(
            scale: Tween(begin: 0.0, end: 1.0).animate(animation),
            child: child,
          ),
        );
      });
}
